package com.example.demo.controller;

import com.example.demo.core.BandWidthData;
import com.example.demo.model.Goods;
import com.example.demo.model.Users;
import com.example.demo.repository.GoodsRepository;
import com.example.demo.repository.UsersRepository;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class DemoController {

    @Autowired
    UsersRepository usersRepository;

    @Autowired
    GoodsRepository goodsRepository;

//    @Autowired
//    private JedisPool jedisPool;

    @RequestMapping("/hello")
    public String hello() {
//        StopWatch sw = new StopWatch();
//        try {
//            sw.start("A");
//            Thread.sleep(1000);
//            sw.stop();
//
//            sw.start("B");
//            Thread.sleep(2000);
//            sw.stop();
//
//            sw.start("C");
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        sw.stop();
//        System.out.println("任务综合信息：" + sw.prettyPrint());
//        System.out.println("总任务时间：" + sw.getTotalTimeMillis());
//        System.out.println("最后一个任务的名称" + sw.getLastTaskName());
//        System.out.println("任务数量：" + sw.getTaskCount());
//        for (StopWatch.TaskInfo t : sw.getTaskInfo()) {
//            System.out.println(t.getTaskName() + " : " + t.getTimeMillis());
//        }
//        System.out.println("shortSummary: " + sw.shortSummary());
//        System.out.println("TotalTimeMillis: " + sw.getTotalTimeMillis());
        return "Hello World!";
    }

    @RequestMapping("/goods/add")
    public Goods goodsAdd(@RequestParam String name, double price) {

        Goods good = Goods.builder().name(name).price(Money.of(CurrencyUnit.of("CNY"), price)).build();
        log.info("Goods {}", good);
        goodsRepository.save(good);
        return good;
    }

    @RequestMapping("/goods/find")
    public List<Goods> goodsFind() {
        List<Goods> goods = goodsRepository.findAll();
        log.info("Goods {}", goods);
        return goods;
    }

    @RequestMapping(value = "/users/find")
    public BandWidthData find() {

        List<Users> users = usersRepository.findAll();
        BandWidthData bind = new BandWidthData();
        bind.setResult("Success");
        bind.setDatas(users);
        return bind;
    }

    @RequestMapping("/users/add")
    public Users usersAdd(@RequestParam String name) {

        Users user = Users.builder().name(name).build();
        log.info("User {}", user);
        usersRepository.save(user);
        return user;
    }

//    @RequestMapping("/redis")
//    public String redis() {
//        try (Jedis jedis = jedisPool.getResource()) {
//            usersRepository.findAll().forEach(c -> {
//                jedis.hset("users",
//                        c.getName(),
//                        Integer.toString(c.getAge()));
//            });
//            Map<String, String> users = jedis.hgetAll("users");
//            log.info("Users: {}", users);
//            String name = jedis.hget("users", "Fiona");
//            log.info("Fiona - {}", name);
//        }
//        return "success";
//    }


}
